package com.easy.admin.system.aspect;

import com.easy.admin.system.annon.FormToken;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.logging.Logger;

/**
 * @author wangbo
 * controller 通知类
 */

@Aspect
@Component
public class ContrAspect {
    private Logger log = Logger.getLogger("ContrAspect");

    @Pointcut("execution(* com.easy.admin.system.controller..*.*(..))")
    public void executeService(){

    }

    @Before("executeService()")
    public void doBeforAdvice(JoinPoint joinPoint) throws Exception{

    }

    @Around("executeService()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable{
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        FormToken annotation = null;
        MethodSignature sign = (MethodSignature) joinPoint.getSignature();
        Method method = sign.getMethod();
        //获取方法上的注解
        annotation = method.getAnnotation(FormToken.class);

        if(annotation==null||annotation.saveToken()==false){
            return joinPoint.proceed();
        }
        else{
            //TODO 处理token验证的业务逻辑

           return "please login";
        }

    }

}
